package com.yaosheng.thread.lock;

import java.util.concurrent.Exchanger;

/**
 * Created by yaosheng on 2021/7/1.
 * Exchange:线程交换器，用户线程之间交换数据
 */
public class TestExchange {

    static Exchanger<String> exchanger = new Exchanger<> ();

    public static void main(String[] args) {

        new Thread ( () -> {
            String s = "T1";
            try{
                s = exchanger.exchange (s);
            }catch (InterruptedException e){
                e.printStackTrace ();
            }
            System.out.println (Thread.currentThread ().getName () + " " + s);
        },"T1").start ();

        new Thread ( () -> {
            String s = "T2";
            try{
                s = exchanger.exchange (s);
            }catch (InterruptedException e){
                e.printStackTrace ();
            }
            System.out.println (Thread.currentThread ().getName () + " " + s);
        },"T2").start ();
    }
}
